package com.azure.bigstore.controller;

import com.azure.bigstore.entity.Order;
import com.azure.bigstore.entity.Token;
import com.azure.bigstore.entity.User;
import com.azure.bigstore.service.TokenService;
import com.azure.bigstore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * Created by liuhuaibin on 2018/1/22.
 */
@RestController
@RequestMapping("/v1")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private TokenService tokenService;
    /**
     * add order
     * @return
     */
    @GetMapping("/userinfo")
    public User getUserInfo(){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String t = request.getHeader("t");
        //String u = request.getHeader("u");
        User user = null;
        if( t == null || "".equals(t)){
            response.setStatus(444);
        }else{
            Token token = tokenService.findByToken(t);
            if(token.getValidityTime().after(new Date())){
                long uid = tokenService.findByToken(t).getUid();
                user = userService.findUserInfo(uid);
            }else{
                response.setStatus(444);
            }
        }
        return user;
    }
    @PostMapping("/userinfo")
    public void saveUserinfo(String addr_sheng, String addr_shi, String addr_qu_xian, String addr_xiang_jiedao, String addr_other,String name,String phone){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String t = request.getHeader("t");
        if( t == null || "".equals(t)){
            response.setStatus(444);
        }else{
            Token token = tokenService.findByToken(t);
            if(token.getValidityTime().after(new Date())){
                long uid = tokenService.findByToken(t).getUid();
                User byId = userService.findById(uid);
                System.out.println("===before save userinfo:"+byId);
                byId.setAddr_sheng(addr_sheng);
                byId.setAddr_shi(addr_shi);
                byId.setAddr_qu_xian(addr_qu_xian);
                byId.setAddr_xiang_jiedao(addr_xiang_jiedao);
                byId.setAddr_other(addr_other);
                byId.setName(name);
                byId.setPhone(phone);
                userService.saveUser(byId);
                System.out.println("===after save userinfo:"+byId);
            }else{
                response.setStatus(444);
            }
        }
    }
    @GetMapping("/usersession")
    public String isLogin(){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String t = request.getHeader("t");

        if(t==null || "".equals(t)){
            return "-1";//token 无效
        }else{
            Token token = tokenService.findByToken(t);
            if(token != null && token.getValidityTime().after(new Date())){
                return ""+token.getUid();
            }else{
                return "-1";//token 无效
            }
        }
    }
}
